package com.jiot.controller.device;

import com.zg.cloud.*;
import com.zg.cloud.dto.AddExploreDto;
import com.zg.cloud.dto.ExploreDto;
import com.zg.cloud.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import req.id.IdReq;
import req.sort.SortReq;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;


/**
 * 探索（pc管理端）
 */
@RestController
@RequestMapping("/device/explore")
@Api(value = "PC端管理系统（探索模块）", tags = "PC端管理系统（探索模块）")
public class ExploreController {

    @Resource
    private ProductService productService;

    @PostMapping("/find/productList")
    @ApiOperation(value = "查询探索（推荐产品信息列表）", notes = "device")
    public BaseResponse findProductList() {
        BaseResponse baseResponse = new BaseResponse();
        List<ExploreDto> selectProductAllResp = productService.findProductList();
        baseResponse.setData(selectProductAllResp);
        return baseResponse;
    }

    @PostMapping("/add/product")
    @ApiOperation(value = "添加(推荐产品)信息", notes = "device")
    public BaseResponse addProduct(AddExploreDto addExploreDto, MultipartFile productfile, MultipartFile productPageFile) {
        BaseResponse baseResponse = new BaseResponse();
        Integer result = productService.addProductAndImg(addExploreDto, productfile, productPageFile);//添加产品
        checkResult(result);
        return baseResponse;
    }

    @PostMapping("/del/product")
    @ApiOperation(value = "删除（推荐产品）", notes = "device")
    public BaseResponse delProduct(@RequestBody IdReq idReq) {
        BaseResponse baseResponse = new BaseResponse();
        Integer result = productService.delProduct(idReq.getId());
        checkResult(result);
        return baseResponse;
    }

    @PostMapping("/sort/product")
    @ApiOperation(value = "排序（推荐产品）(0-升序，1-降序)", notes = "device")
    @Transactional
    public BaseResponse sortProduct(@RequestBody SortReq sortReq) {
        BaseResponse baseResponse = new BaseResponse();
        if (Objects.isNull(sortReq))
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        Boolean result = productService.sortProductRe(sortReq.getId(), sortReq.getSortFlag());
        checkResult(result);
        return baseResponse;
    }

    /**
     * 结果教研
     *
     * @param result
     * @return
     */
    private Boolean checkResult(Integer result) {
        if (result <= 0) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        return true;
    }

    /**
     * 结果校验
     *
     * @param result
     * @return
     */
    private Boolean checkResult(Boolean result) {
        if (result.equals(false)) {
            throw new BusinessException(ErrorCode.PARAM_ERROR);
        }
        return true;
    }
}
